import { BT_State } from "../../BTDefine";
import { Composite } from "./Composite";

export class Parallel extends Composite {
    public onEnter(): void {

    }

    public onExit(): void {

    }

    public onProcess(dt: number): void {
        let failureNum = 0;
        let successNum = 0;

        for (let i = 0; i < this._children.length; i++) {
            const child = this._children[i];
            let ret = child.getState();

            if (ret == BT_State.RUNNING || ret == BT_State.NONE) {
                ret = child.tick(dt);
            }

            if (ret == BT_State.SUCCESS) {
                successNum++;
            } else if (ret == BT_State.FAILURE) {
                failureNum++;
            }

            if ((successNum + failureNum) == this._children.length) {
                this.setResult(failureNum == 0 ? BT_State.SUCCESS : BT_State.FAILURE);
            }
        }
    }
}